﻿using System.Windows;
using CommunityToolkit.Mvvm.DependencyInjection;
using CommunityToolkit.Mvvm.Messaging;
using FanaticRock.Core.ServiceInterfaces;
using FanaticRock.Events;
using FanaticRock.Helpers;
using FanaticRock.ViewModels;
using Hardcodet.Wpf.TaskbarNotification;

namespace FanaticRock
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application, IRecipient<AppMessageEventArgs>
    {
        private Views.ShellWindow _mainWindow;
        private Views.Tray.TrayToolWindow _trayToolWindow;

        public App()
        {
            ShutdownMode = ShutdownMode.OnMainWindowClose;
            WeakReferenceMessenger.Default.Register(this);
            DispatcherUnhandledException += App_DispatcherUnhandledException;
        }

        private void App_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
        {
            MessageBox.Show($"{sender.ToString} {e.Exception}", "遇到异常");
            e.Handled= true;
        }

        public void Receive(AppMessageEventArgs message)
        {
            switch (message.Type)
            {
                case AppMessageEventArgs.MessageType.Default:
                    break;
                case AppMessageEventArgs.MessageType.Message:
                    ReceiveMessage(message);
                    break;
                case AppMessageEventArgs.MessageType.NavWindow:
                    ReceiveNavWindow(message.WindowType);
                    break;
                case AppMessageEventArgs.MessageType.Other:
                    break;
                default:
                    break;
            }
        }

        private void ReceiveMessage(AppMessageEventArgs message)
        {
            _mainWindow.ReceiveMessage(message);

        }

        private void ReceiveNavWindow(AppMessageEventArgs.WindowEnum windowType)
        {
            switch (windowType)
            {
                case AppMessageEventArgs.WindowEnum.MainWindow:
                    _trayToolWindow.Hide();
                    _mainWindow.Show();
                    break;
                case AppMessageEventArgs.WindowEnum.TrayToolWindow:
                    _trayToolWindow.Show();
                    _mainWindow.Hide();
                    break;
                case AppMessageEventArgs.WindowEnum.CloseMainWindow:
                    _mainWindow.Hide();
                    break;
                case AppMessageEventArgs.WindowEnum.CloseTrayToolWindow:
                    _trayToolWindow.Hide();
                    break;
                default:
                    break;
            }
        }

        protected override async void OnExit(ExitEventArgs e)
        {
            var playListService = Ioc.Default.GetService<IPlayListService>();
            await playListService.SaveList(false);
            base.OnExit(e);
        }

        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);
            Init();

            _mainWindow = Ioc.Default.GetService<Views.ShellWindow>();
            MainWindow = _mainWindow;

            MainWindow.Show();

            _trayToolWindow = Ioc.Default.GetService<Views.Tray.TrayToolWindow>();

            // 显示托盘。
            var notifyIcon = Resources["MyNotifyIcon"] as TaskbarIcon;
            notifyIcon.Visibility = Visibility.Visible;


        }
    }

}
